package com.zys.el.time.controller;

import com.zys.el.time.common.Result;
import com.zys.el.time.dto.Page;
import com.zys.el.time.entity.Topic;
import com.zys.el.time.service.TopicService;
import com.zys.el.time.vojo.TopicVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "话题接口")
@Slf4j
@RestController
@RequestMapping("/topic")
@CrossOrigin
public class TopicController {
    @Resource
    private TopicService topicService;

    @GetMapping("/page")
    @ApiOperation("获取话题分页By频道id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelId", value = "频道id", required = true),
            @ApiImplicitParam(name = "page", value = "页码", required = true),
            @ApiImplicitParam(name = "size", value = "页大小", required = true)
    })
    public Result<Page<TopicVo>> getPage(@RequestParam("channelId") String channelId,
                                         @RequestParam("page") Integer page,
                                         @RequestParam("size") Integer size) {
        log.debug(channelId, page, size);
        Page<TopicVo> topicPage = topicService.getPageByChannelId(Long.valueOf(channelId), page, size);
        return Result.ok(topicPage);
    }

    @PostMapping("/add")
    @ApiOperation("添加话题")
    public Result add(@RequestBody Topic topic) {
        log.debug(String.valueOf(topic));
        topicService.addTopic(topic);
        return Result.ok("添加成功");
    }

    @DeleteMapping("/del/{topicId}")
    @ApiOperation("删除话题")
    @ApiImplicitParam(name = "topicId", value = "话题id", required = true)
    public Result del(@PathVariable String topicId) {
        log.debug(topicId);
        if (topicService.removeById(topicId)) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    @PutMapping("/modify")
    @ApiOperation("修改话题")
    public Result modify(@RequestBody Topic topic) {
        log.debug(String.valueOf(topic));
        if (topicService.updateById(topic)) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }
}
